package com.carlinfo.stu_ques.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * 
 * @author 90711
 *
 */
public class FileUtil
{

	/**
	 * 将Map写到文件中
	 * 
	 * @param os
	 * @param obj 要写的对象
	 * @return
	 */
	public boolean writeObject(OutputStream os, Object obj)
	{
		BufferedOutputStream bfr = new BufferedOutputStream(os);
		ObjectOutputStream oos = null;
		try
		{
			oos = new ObjectOutputStream(bfr);

			oos.writeObject(obj);

			oos.flush();
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (oos != null)
				{
					oos.close();
					oos = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}

		return false;
	}

	/**
	 * 将文件中的对象读出来
	 * 
	 * @param is
	 * @return
	 */
	public Object readObject(InputStream is)
	{
		BufferedInputStream bis = new BufferedInputStream(is);
		ObjectInputStream ois = null;
		try
		{
			ois = new ObjectInputStream(bis);
			return ois.readObject();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (ois != null)
				{
					ois.close();
					ois = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}
}
